﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using sconSDK;
using System.Threading;
using System.Diagnostics;

namespace sconApp
{
    public partial class Form1 : Form
    {
        private Scon m_scon = null;

        public Form1()
        {
            InitializeComponent();

            if (Connect() == true)
            {
                servoCtl1.Init(m_scon, 1);
                servoCtl2.Init(m_scon, 2);
                servoCtl3.Init(m_scon, 3);
                servoCtl4.Init(m_scon, 4);
                servoCtl5.Init(m_scon, 5);
                servoCtl6.Init(m_scon, 6);
                servoCtl7.Init(m_scon, 7);
                servoCtl8.Init(m_scon, 8);

                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Start();
            }
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            lblLineNum.Text = m_scon.CurrentScriptLine.ToString();
            lblLoop.Text = m_scon.CurrentIteration.ToString();
        }

        private bool Connect()
        {
            Stopwatch sw = Stopwatch.StartNew();
            m_scon = new Scon();

            if (m_scon.Connect() == true)
            {
                sw.Stop();
                lblStatus.Text = "Connected to " + m_scon.Version + " on " + m_scon.PortName + " (" + sw.ElapsedMilliseconds + " ms)";
                lblStatus.Refresh();
            }
            else
            {
                return false;
            }

            return true;
        }

     

        private void btnConnect_Click(object sender, EventArgs e)
        {

            Output op1 = new Output(m_scon, 1);
            Output op2 = new Output(m_scon, 2);
            Output led = new Output(m_scon, 15);
            op1.Value = Output.OutputState.On;
            Thread.Sleep(1000);
            op1.Value = Output.OutputState.Off;

            for (int i = 0; i < 3; i++)
            {
                servoCtl1.Servo.Value = 9000;
                System.Threading.Thread.Sleep(500);

                servoCtl1.Servo.Value = 12000;

                servoCtl2.Servo.Value = 12500;
                servoCtl3.Servo.Value = 12500;
                System.Threading.Thread.Sleep(500);

                servoCtl1.Servo.Value = 15000;

                led.Value = Output.OutputState.On;
                System.Threading.Thread.Sleep(500);
                led.Value = Output.OutputState.Off;

                servoCtl1.Servo.Value = 22500;
                servoCtl2.Servo.Value = 21500;
                servoCtl3.Servo.Value = 22500;
                System.Threading.Thread.Sleep(500);
            }

            //for (int i = 0; i < 10; i++)
            //{
            //    s.ToggleOutput(Scon.OutPut.Op1, true);
            //    s.ToggleOutput(Scon.OutPut.Op2, true);
            //    s.ToggleOutput(Scon.OutPut.Op1, false);
            //    s.ToggleOutput(Scon.OutPut.Op2, false);


        }

 

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (m_scon !=null)
            {
                m_scon.ScriptRun();
            }
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (m_scon != null)
            {
                m_scon.ScriptPause();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (m_scon != null)
            {
                m_scon.ScriptStop();
            }
        }

 
    }
}
